<?php 
namespace Home\Controller;
use \Think\Controller;

class CollectionsController extends HomeController
{
    public function index(){
        $collections = D('');

        $this->assign('title','我关注的收藏夹 -知乎');
        $this->display('Collections/index');
    }
    public function mine(){
        if(IS_AJAX)
        {
            $edc['sname']=$_POST['sname'];
            $edc['fondcontent']=$_POST['fondcontent'];
            $edc['user_id']=$_SESSION['home']['id'];
            $edc['is_private']=$_POST['is_private'];
            $write = M('store')->add($edc);

        } 
            $black = M('store')->where(array('user_id'=>$_SESSION['home']['id']))->order('id desc')->select();
            // var_dump($black);
            $this->assign('black',$black);
            $this->assign('title','我关注的收藏夹 -知乎');
            $this->display('Collections/mine');
    }

    public function detail(){
        if(!empty($_GET['id']) && intval($_GET['id']) > 0) {
                $id = intval($_GET['id']);
            } else {
                $this->error('id不合法');
            }

        $ten = M('collect');
        $nine = $ten->where('user_id='.$_SESSION['home']['id'])->select();
        // var_dump($nine);
        $eig=[];
        $xiu=[];
        foreach ($nine as $val) {
            $eig[]=$val['answer_id'];
            $xiu[]=$val['store_id'];
        }

        // var_dump($xiu);
        $xius = implode(',',$xiu);
        // var_dump($xius);
        $sev = implode(',',$eig);
        // var_dump($sev);
        $one = M('answer');
      // echo $two = $one->field('za.id zaid,zs.sname zsn,zs.fondcontent zsf,za.part zap,zq.name zqn')->table('zhihu_answer za,zhihu_question zq,zhihu_store zs,zhihu_user zu,zhihu_collect zc')->where("zs.id =$id AND za.id IN ($sev) AND za.question_id = zq.id AND zc.store_id=$id AND zu.id=".$_SESSION['home']['id']."")->fetchSql()->select();

        // 收藏的问题和答案
        $two=$one->field('a.id aid, a.part apart, a.datetime atime, q.id qid, q.name qname')->table('zhihu_collect c, zhihu_answer a, zhihu_question q')->where('c.store_id='.$id.' AND a.id=c.answer_id AND q.id=a.question_id')->select();
            // var_dump($two);
        // 收藏的文章
        $bbai=$one->field('ar.id arid, ar.title artitle, ar.datetime artime')->table('zhihu_collect c, zhihu_article ar')->where('c.store_id='.$id.' AND ar.id=c.article_id')->select();

        $dand = M('store')->where("id=$id")->select();
        // var_dump($dand);
        // var_dump($_GET['id']);
        // var_dump($two);
        $sto = M('store')->select();
        // var_dump($sto);
        $this->assign('dand',$dand);
        $this->assign('sto',$sto);
        $this->assign('two',$two);
        $this->assign('title', '收藏 - 知乎');
        $this->display('Collections/detail');
    }
    public function qushou(){
        // $data['id']=$_GET['id'];
        // var_dump($data);
        $ret = M('collect');
     $sca = $ret->where(array('answer_id'=>$_GET['id'],'user_id'=>$_SESSION['home']['id']))->delete();

    }
    public function edit(){
        $eit['sname']=$_GET['sname'];
        $eit['fondcontent']=$_GET['fondcontent'];
        // echo $_GET['sname'];
        // echo $_GET['fondcontent'];
        $bianj = M('store')->where(array('id'=>$_GET['id']))->save($eit);
    }
    public function del(){
       $niu = M('store')->where(array("id"=>$_GET['id']))->delete();
       // if ($niu) {
       //     $this->success('删除成功','U(Collections/mine)');
       // }else{
       //  $this->error();
       // }
    }
}



